home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 396 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.5 KB

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: mtr <M.T.Russell@ukc.ac.uk>
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Article for comp.std.c++: Eliminating #ifdef: if const
  5. Date: 20 Feb 1996 15:44:54 GMT
  6. Organization: University of Kent at Canterbury
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <199602201003.KAA15992@condor.ukc.ac.uk>
  9. References: <199602160807.IAA06126@condor.ukc.ac.uk>
  10. NNTP-Posting-Host: taumet.eng.sun.com
  11. Content-Type: text
  12. In-Reply-To: <4gb0a4$sa3@fido.asd.sgi.com>
  13. Content-Length: 1734
  14. X-Lines: 65
  15. Originator: clamage@taumet
  16.  
  17. Shankar Unni (shankar@engr.sgi.com) wrote:
  18. >No dice.
  19. >
  20. >Often, an #ifdef covers just a small part of an expression that may be
  21. >different between different environments, or some such small differences:
  22. >
  23. >   if (some condition) {
  24. >#ifdef SOMEIMPL
  25. >    if (some other condition) {
  26. >#endif /* SOMEIMPL */
  27. >    /* code */
  28. >    /* code */
  29. >    /* code */
  30. >#ifdef SOMEIMPL
  31. >    }
  32. >#endif /* SOMEIMPL */
  33. >   }
  34.  
  35. Personally I would be quite happy to see that style of #ifdef'ing
  36. disappear.  I've seen too much code rendered totally impenetrable by
  37. just this technique.  I would write the above as:
  38.  
  39.     if (some condition) {
  40.         if const (SOMEIMPL) {
  41.             if (some other condition)
  42.                 code();
  43.         }
  44.         else const {
  45.             code();
  46.         }
  47.     }
  48.  
  49. where code() is the code wrapped up in a (possibly inline) function.
  50. Alternatively, if you can persuade your compiler not to complain about
  51. a constant if() controlling expression:
  52.  
  53.     if (some condition) {
  54.         bool doit;
  55.  
  56.         if const (SOMEIMPL) {
  57.             doit = some other condition;
  58.         }
  59.         else const {
  60.             doit = true;
  61.         }
  62.  
  63.         if (doit) {
  64.             code
  65.         }
  66.     }
  67.  
  68. Of course in reality you would hope to bury all the implementation
  69. dependent stuff behind a portability layer.
  70.  
  71. >Also, the #ifdefs serve a purpose in pointing out the fact that this piece
  72. >of code is system-dependent.  The syntax you propose hides its warts rather
  73. >too well, and make it easy for a reader to miss the significant piece of
  74. >information that this area of code is system-dependent.
  75.  
  76. I agree that this is a problem, but I would argue that sprinkling `if
  77. const' (or #ifdef) throughout the main body of the code is bad style,
  78. and thus comes under the `any feature can be misused' getout.  Misuse
  79. like this is easily spotted: `if const' is just as visible to grep as #if.
  80.  
  81. Mark
  82.  
  83. [ To submit articles: Try just posting with your newsreader.  If that fails,
  84.               use mailto:std-c++@ncar.ucar.edu
  85.   FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  86.   Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  87.   Comments? mailto:std-c++-request@ncar.ucar.edu
  88. ]
  89.